<template>
  <VPage class="app-container">
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <multiInput class="w-50" v-model="form.receipt_number" placeholder="采购单号"></multiInput>
        <multiInput class="w-50" v-model="form.tracking_number" placeholder="快递单号"></multiInput>
        <SelectWarehouse class="selectClass" v-model="form.warehouse_id" placeholder="仓库" :multiple="true">
        </SelectWarehouse>
        <select-personnel class="w-40" v-model="form.receive_user_id" placeholder="点数人"></select-personnel>
        <!-- <el-select class="selectClass w-30" v-model="form.type" placeholder="入库类型" clearable>
          <el-option v-for="item in options['stockin.shelveOrder']?.[0].orderShelveType" :key="item.value"
            :label="item.label" :value="item.value"></el-option>
        </el-select> -->
        <el-select class="selectClass w-30" v-model="form.urgent_status" placeholder="是否加急" clearable>
          <el-option v-for="item in options['stockin.shelveOrder']?.[0].urgentStatus" :key="item.value"
            :label="item.label" :value="item.value"></el-option>
        </el-select>
        <el-select class="selectClass w-30" v-model="form.status" placeholder="处理状态" clearable>
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
        <VGroup>
          <el-select class="selectClass" v-model="form.timeRange.type" style="max-width: 100px">
            <el-option v-for="item in timeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-date-picker v-model="form.timeRange.value" type="daterange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime()" style="max-width: 300px" />
        </VGroup>
        <multiInput class="w-50" v-model="form.sku" placeholder="SKU/自定义SKU"></multiInput>
      </template>
      <template #toolbar_btns>
        <el-button type="primary" @click="handleConfirm()">确认</el-button>
        <el-button @click="handleCancel()">取消</el-button>
        <el-button class="ml-auto" @click="handleExport()"><svg-icon icon-class="import" />导出</el-button>
      </template>
    </VTable>
    <DialogImage ref="DialogImageRef"></DialogImage>
    <derived-center ref="DerivedCenterRef"></derived-center>

  </VPage>
</template>
<script setup name="StorageReceive-abnormal" lang="jsx">
import { ElMessage } from 'element-plus'
import { ref, reactive, toRefs } from 'vue'
import { getEnums } from '@/api/public'
import { queryListOrder, affirmAbnormal, cancelAbnormal } from './api'
import multiInput from '@/components/multiInput';
import SelectWarehouse from '@/components/SelectWarehouse'
import DialogImage from '@/components/DialogImage'

const timeOption = [
  { label: "下单时间", value: "order_time" },
  { label: "创建时间", value: "create_time" },
]

const statusOptions = [
  {
    label: '未处理',
    value: 0
  },
  {
    label: '确认',
    value: 1
  },
  {
    label: '取消',
    value: 2
  }
]

const tableRef = ref()
const tableOptins = reactive({
  rowConfig: { height: 'auto' },
  showHeaderOverflow: false,
  showOverflow: false,
  formConfig: {
    data: {
      timeRange: { type: 'order_time', value: [], range: true },
    }
  },
  columns: [
    { type: 'checkbox', width: 50, fixed: 'left' },
    {
      type: "seq", title: '序号', width: 80
    },
    {
      field: 'receipt_number', title: '采购单号', width: 180
    },
    {
      field: 'express_json', title: '快递单号', width: 180
    },
    {
      field: 'sku', title: 'SKU', width: 180
    },
    {
      field: 'num', title: '采购数量', width: 120
    },
    {
      field: 'abnormal_num', title: '差异数量', width: 120
    },
    {
      field: 'urgent_status_cn', title: '是否加急', width: 120
    },
    {
      field: 'receive_status_cn', title: '收货状态', width: 120
    },
    {
      field: 'status_cn', title: '单据类型', width: 120
    },
    {
      field: 'order_time', title: '下单时间', width: 180
    },
    {
      field: 'receive_user', title: '点数人', width: 160
    },
    {
      field: 'purchase_user', title: '采购员', width: 160
    },
    {
      field: 'create_time', title: '创建时间', width: 180
    },
    {
      field: 'platform_order_no', title: '平台单号', width: 180
    },
    {
      field: 'business_type_cn', title: '业务类型', width: 120
    },
    {
      field: 'remark', title: '备注', width: 160
    },
    {
      title: '操作', width: "100", fixed: "right", slots: {
        default: ({ row }) => {
          return <div class="table-option">
            <div>
              <el-button type="primary" link onClick={() => handlePhoto(row.receive_detail_id)}>异常照片</el-button>
            </div>
          </div >
        }
      }
    }
  ],
  proxyConfig: {
    seq: true,
    props: {
      result: 'data',
      total: 'total',
    },
    ajax: {
      query: async ({ page, form }) => {
        const res = await queryListOrder({ ...form, ...page })
        return new Promise(function (resolve) {
          resolve({
            data: res.data,
            total: res.total
          })
        })
      }
    }
  }
})
const query = () => {
  tableRef.value.query()
}

const data = reactive({
  options: [],
})
const { options } = toRefs(data)

// 获取枚举类
const fetchOption = async () => {
  getEnums({
    version: "v1", "name": [
      "stockin.shelveOrder"
    ]
  }).then(res => {
    options.value = res.data
  })
}
fetchOption()

// 批量确认
const handleConfirm = async () => {
  const records = getSelectRow()
  if (records.length == 0) {
    ElMessage.error('至少选择一项')
    return
  }
  const ids = records.map(i => i.id)
  try {
    const res = await affirmAbnormal({
      ids
    })
    ElMessage.success(res.message)
    query()
  } catch (e) {
    throw Error(e)
  }
}

// 批量取消
const handleCancel = async () => {
  const records = getSelectRow()
  if (records.length == 0) {
    ElMessage.error('至少选择一项')
    return
  }
  const ids = records.map(i => i.id)
  try {
    const res = await cancelAbnormal({
      ids
    })
    ElMessage.success(res.message)
    query()
  } catch (e) {
    throw Error(e)
  }
}

const DerivedCenterRef = ref()
const handleExport = (val) => {
  let condition = tableRef.value.getQueryForm()
  DerivedCenterRef.value.open({
    id: 63,
    tag_name: 'wms',
    condition
  })
}

const DialogImageRef = ref()
const handlePhoto = (id) => {
  DialogImageRef.value.open(1,id)
}

//获取已选中的行
const getSelectRow = () => {
  return tableRef?.value.$table.getCheckboxRecords()
}
</script>